package com.woniu.houtai.controller;


import com.woniu.houtai.entity.Business;
import com.woniu.houtai.entity.Shop;
import com.woniu.houtai.service.BusinessService;
import com.woniu.houtai.service.OrderService;
import com.woniu.houtai.utils.CookieUtils;
import com.woniu.houtai.utils.JwtUtils;
import com.woniu.houtai.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author firstGroup
 * @since 2023-02-22 09:45:58
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    BusinessService businessService;

    private Business getCurrentBusiness(HttpServletRequest request) {
        //获取已登录商家信息
        // 1.先从request中拿到token
        // 2.从token中获取用户名
        // 3.根据用户名查询到用户信息
        String userTokenFromCookie = CookieUtils.getUserTokenFromCookie(request);
        String account = JwtUtils.getAccountWithoutException(userTokenFromCookie);
        return businessService.getBusinessByName(account);
    }

    //根据shopId查到当前订单的信息
    @RequestMapping("merchantOrderVoList")
    @ResponseBody
    public List<OrderVo> merchantOrderVoList(Integer shopId,Integer status){
        System.out.println(shopId);
        System.out.println(status);
        List<OrderVo>mOrderVoList=orderService.selectOrderVo(shopId,status);
        System.out.println("41-mOrderVoList="+mOrderVoList);
        System.out.println("41-length="+mOrderVoList.size());
        return mOrderVoList;
    }

    //商家手动操作订单状态
    @RequestMapping("operation")
    @ResponseBody
    public String operation(Integer orderId, Integer states) throws InterruptedException {
        System.out.println("获取状态："+states);
        orderService.updateOrderState(orderId,states);
        System.out.println("商家:"+orderId+"号订单操作完成");
        return"ok";
    }



    //商家自动接单
    //执行修改当前商家的数据库订单将3改为4
    @RequestMapping("automaticOrder")
    @ResponseBody
    public void automaticOrder(Integer shopId) throws InterruptedException {
        System.out.println("自动接单"+shopId);
        orderService.updateOrders(shopId);

    }
}

